package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

import com.sun.awt.AWTUtilities;
import gui.UI;
import implementation.*;
import variable.Counter;

public class Login {
    public static void login(){
        GraphicsDevice graphDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        DisplayMode screenSize = graphDevice.getDisplayMode();
        //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Empty-Chat");
        //UI.BACKGROUND_IMG.setImage(UI.BACKGROUND_IMG.getImage().getScaledInstance(1280,720,Image.SCALE_DEFAULT));
        frame.setSize(UI.BACKGROUND_IMG.getIconWidth(),UI.BACKGROUND_IMG.getIconHeight()+33);
        frame.setLocationRelativeTo(null);
        //frame.setLocation((int)(screenSize.getWidth()/5.5),(int)(screenSize.getHeight()/5.5));
        frame.setLayout(new BorderLayout());
        frame.setUndecorated(true);
        //frame.setResizable(true);
        AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 30.0D,
                30.0D));
        frame.setIconImage(UI.LOGO.getImage());
        frame.setVisible(true);

        //组件
        NewLabel label1 = new NewLabel("Empty-Chat","宋体",20);
        NewLabel label2 = new NewLabel("注册","宋体",UI.FONT_COLOR2,12);
        NewLabel label3 = new NewLabel("忘记密码","宋体",UI.FONT_COLOR2,12);
        UI.Head_img.setImage(UI.Head_img.getImage().getScaledInstance(120,120,Image.SCALE_DEFAULT));
        NewLabel label4 = new NewLabel(UI.Head_img, new Dimension(120,120));
        //label4.setLocation((UI.BACKGROUND_IMG.getIconWidth()/2 - UI.Head_img.getIconWidth()/2),100);

        UI.CLOSE.setImage(UI.CLOSE.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
        UI.CLOSE_C.setImage(UI.CLOSE_C.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));
        UI.CLOSE_S.setImage(UI.CLOSE_S.getImage().getScaledInstance(20,20,Image.SCALE_DEFAULT));

        NewButton button1 = new NewButton("登录");
        NewButton2 button2 = new NewButton2(UI.CLOSE_S, UI.CLOSE, new Dimension(20, 20));
        NewButton2 button3 = new NewButton2(UI.CLOSE_S,UI.CLOSE,new Dimension(20, 20));
        NewButton2 button4 = new NewButton2(UI.CLOSE_C,UI.CLOSE,new Dimension(20, 20));

        NewCombobox comboBox = new NewCombobox();
        String[] qq = {"12345","23456","345567","45678"};
        for (int i=0;i<4;i++){
            comboBox.addItem(qq[i]);
        }

        NewPasswordfield passwordField = new NewPasswordfield(15);

        JCheckBox checkbox1 = new JCheckBox("记住密码");
        JCheckBox checkbox2 = new JCheckBox("自动登录");
        checkbox1.setOpaque(false);
        checkbox1.setForeground(UI.FONT_COLOR2);
        checkbox2.setOpaque(false);
        checkbox2.setForeground(UI.FONT_COLOR2);

        //面板
        JPanel panel1 = new JPanel();
        NewPanel panel2 = new NewPanel(UI.BACKGROUND_IMG);

        Box box1 = Box.createVerticalBox();
        Box box2 = Box.createHorizontalBox();
        Box box3 = Box.createHorizontalBox();

        panel1.setVisible(true);
        panel2.setVisible(true);

        //布局
        panel1.setBackground(UI.BACKGROUND);
        panel1.add(label1);
        //panel1.add(Box.createHorizontalStrut(UI.BACKGROUND_IMG.getIconWidth()-200));
        panel1.add(Box.createHorizontalStrut(frame.getWidth()-200));
        panel1.add(button2);
        panel1.add(button3);
        panel1.add(button4);

        box2.add(Box.createHorizontalStrut(50));
        box2.add(checkbox1);
        box2.add(Box.createHorizontalStrut(73));
        box2.add(label2);

        box3.add(Box.createHorizontalStrut(50));
        box3.add(checkbox2);
        box3.add(Box.createHorizontalStrut(50));
        box3.add(label3);


        /*
        box1.add(label4);
        box1.add(Box.createVerticalStrut(20));
        box1.add(comboBox);
        box1.add(Box.createVerticalStrut(20));
        box1.add(passwordField);
        box1.add(Box.createVerticalStrut(20));
        box1.add(box2);
        box1.add(Box.createVerticalStrut(20));
        box1.add(box3);
        box1.add(Box.createVerticalStrut(20));
        box1.add(button1);

        panel2.add(box1);

         */

        //y轴12等分窗口
        panel2.setLayout(null);

        label4.setSize(UI.Head_img.getIconWidth(),UI.Head_img.getIconHeight());
        comboBox.setSize(UI.TEXTFILEL_DIMESION);
        passwordField.setSize(UI.TEXTFILEL_DIMESION);
        box2.setSize(UI.TEXTFILEL_DIMESION);
        box3.setSize(UI.TEXTFILEL_DIMESION);
        button1.setSize((int)UI.TEXTFILEL_DIMESION.getWidth()-30,(int)UI.TEXTFILEL_DIMESION.getHeight());

        label4.setLocation(frame.getWidth()/2-label4.getWidth()/2,frame.getHeight()/12);
        comboBox.setLocation(frame.getWidth()/2-comboBox.getWidth()/2,frame.getHeight()*3/12+40);
        passwordField.setLocation(frame.getWidth()/2-passwordField.getWidth()/2,frame.getHeight()*5/12);
        box2.setLocation(frame.getWidth()/2-box2.getWidth()/2,frame.getHeight()*7/12-50);
        box3.setLocation(frame.getWidth()/2-box3.getWidth()/2,frame.getHeight()*8/12-60);
        button1.setLocation(frame.getWidth()/2-button1.getWidth()/2,frame.getHeight()*8/12);

        panel2.add(label4);
        panel2.add(comboBox);
        panel2.add(passwordField);
        panel2.add(box2);
        panel2.add(box3);
        panel2.add(button1);


        //加入窗口

        frame.add(panel1,BorderLayout.NORTH);
        frame.add(panel2,BorderLayout.CENTER);


        //功能
        button1.addActionListener(e -> {});
        button2.addActionListener(e -> {frame.setExtendedState(JFrame.ICONIFIED);});//最小化到托盘
        button3.addActionListener(e -> {
            /*if (! Counter.big){
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                Counter.big = true;
            }else {
                frame.setExtendedState(JFrame.NORMAL);
            }

             */
            if(frame.getExtendedState() != frame.MAXIMIZED_BOTH) {
                frame.setExtendedState(frame.MAXIMIZED_BOTH);
                AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                        0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 0.0D,
                        0.0D));
            }else {
                AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(
                        0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 30.0D,
                        30.0D));
                frame.setExtendedState(frame.NORMAL);
            }

        });//最大化
        button4.addActionListener(e -> System.exit(0));




        //登录成功界面

        //SwingUtilities.invokeLater(Gui::gui);
    }
}
